package com.hunan.api.catv.service.biz.upgrade;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunan.api.catv.common.CacheName;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.po.Upgrade;
import com.hunan.api.catv.entity.vo.UpgradeVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.impl.upgrade.UpgradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;


@CacheConfig(cacheNames = CacheName.upgrade)
@Service
public class UpgradeBiz extends ResultService {

    @Autowired
    UpgradeService upgradeService;


    @Cacheable(unless = "#result == null")
    public UpgradeVo lastVersion(Integer channel, Integer clientVersion, String cName) {
        Upgrade upgrade = new Upgrade();
        upgrade.setChannel(channel);
        upgrade.setState(1);
        if (!ObjectUtils.isEmpty(cName)) {
            upgrade.setcName(cName);
        }
        List<UpgradeVo> list = upgradeService.list(upgrade);
        if (!ObjectUtils.isEmpty(list)) {
            UpgradeVo upgradeVo = list.get(0);
            if (clientVersion < upgradeVo.getVersionCode()) {
                return upgradeVo;
            }
        }
        return new UpgradeVo();
    }


    public PageInfo<UpgradeVo> page(int pageNum, int pageSize, Integer channel) {
        PageHelper.startPage(pageNum, pageSize);
        Upgrade upgrade = new Upgrade();
        upgrade.setChannel(channel);
        List<UpgradeVo> list = upgradeService.list(upgrade);
        return new PageInfo<UpgradeVo>(list);
    }

    public UpgradeVo info(Integer id) {
        return upgradeService.getById(id);
    }

    public ServiceResult add(Upgrade upgrade) {
        return upgradeService.add(upgrade);
    }

    public ServiceResult edit(Upgrade upgrade) {
        if (ObjectUtils.isEmpty(upgrade.getId())) {
            return buildFail("", "id not null");
        }
        return upgradeService.update(upgrade);
    }

}
